﻿using System;

namespace LargestArea
{
    class LargestArea
    {
        static int founded = 0;
        static void Check(int?[,] matrix, int rowIndex, int colIndex, int? number)
        {
            int rows = matrix.GetLength(0) - 1,
                cols = matrix.GetLength(1) - 1;

            if (rowIndex > 0)
            {
                // + o +
                // + o +
                // + + +
                int topRowIndex = rowIndex - 1;
                if (matrix[topRowIndex, colIndex] == number)
                {
                    founded++;
                    matrix[topRowIndex, colIndex] = null;
                    Check(matrix, topRowIndex, colIndex, number);
                }
            }
            if (0 < colIndex)
            {
                // + + +
                // o o +
                // + + +
                int leftColIndex = colIndex - 1;
                if (matrix[rowIndex, leftColIndex] == number)
                {
                    founded++;
                    matrix[rowIndex, leftColIndex] = null;
                    Check(matrix, rowIndex, leftColIndex, number);
                }
            }
            if (rowIndex < rows)
            {
                // + + +
                // + o +
                // + o +
                int downRowIndex = rowIndex + 1;
                if (matrix[downRowIndex, colIndex] == number)
                {
                    founded++;
                    matrix[downRowIndex, colIndex] = null;
                    Check(matrix, downRowIndex, colIndex, number);
                }
            }
            if (colIndex < cols)
            {
                // + + +
                // + o o
                // + + +
                int rightColIndex = colIndex + 1;
                if (matrix[rowIndex, rightColIndex] == number)
                {
                    founded++;
                    matrix[rowIndex, rightColIndex] = null;
                    Check(matrix, rowIndex, rightColIndex, number);
                }
            }
        }
        static void Main()
        {
            int bestLength = 0;
            int? bestNumber = 0;
            int?[,] matrix =
            {
                { 1, 3, 2, 3, 3, 3 },
                { 3, 3, 3, 2, 2, 3 },
                { 2, 3, 2, 2, 3, 3 },
                { 4, 3, 2, 3, 3, 1 },
                { 4, 3, 3, 3, 1, 1 }
            };

            for (int row = 0, rowsLength = matrix.GetLength(0); row < rowsLength; row++)
            {
                for (int col = 0, colsLength = matrix.GetLength(1); col < colsLength; col++)
                {
                    int? matrixNumber = matrix[row, col];
                    founded = 0;
                    if (matrixNumber != null)
                    {
                        Check(matrix: matrix, rowIndex: row, colIndex: col, number: matrixNumber);
                        if (bestLength < founded)
                        {
                            bestLength = founded;
                            bestNumber = matrixNumber;
                        }
                    }
                }
            }
            Console.WriteLine("BestLength: {0}, number: {1}", bestLength, bestNumber);
        }
    }
}